package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;




    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //判断增添的是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        if(dishId != null) {
            //查找购物车中是否已经存在
            ShoppingCart shoppingCart1 =  shoppingCartMapper.getByDishId(dishId,userId);
            if(shoppingCart1 != null) {
                //存在则直接数量+1,金额翻倍
                shoppingCartMapper.increaseDish(dishId,userId);
            }
            else
            {
                //添加进去
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.add(shoppingCart);
            }
        }
        Long setmealId = shoppingCartDTO.getSetmealId();
        if(setmealId != null) {
            //查找购物车中是否已经存在
            ShoppingCart shoppingCart1 =  shoppingCartMapper.getBySetMealId(setmealId,userId);
            if(shoppingCart1 != null)
            {
                //存在数量+1，金额翻倍
                shoppingCartMapper.increaseSetMeal(setmealId,userId);
            }
            else
            {
                //不存在s
                Integer id = Math.toIntExact(setmealId.longValue());
                Setmeal setmeal = setmealMapper.getById(id);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.add(shoppingCart);
            }
        }
    }

    /**
     * 查看购物车接口
     *
     * @return
     */
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();

        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        if(dishId != null) {
            ShoppingCart shoppingCart = shoppingCartMapper.getByDishId(dishId, userId);
            Integer number = shoppingCart.getNumber();

            Dish dish = dishMapper.getById(dishId);
            BigDecimal price = dish.getPrice();

            if(number >= 2)
            {
                shoppingCartMapper.sub(dishId,setmealId,price,userId);
            }else
            {
                shoppingCartMapper.del(dishId,setmealId,userId);
            }
        }
        if(setmealId != null) {
            ShoppingCart shoppingCart = shoppingCartMapper.getBySetMealId(setmealId,userId);
            Integer number = shoppingCart.getNumber();

            Setmeal setmeal = setmealMapper.getById((int) setmealId.longValue());
            BigDecimal price = setmeal.getPrice();

            if(number >= 2)
            {
                shoppingCartMapper.sub(dishId,setmealId, price, userId);
            }else
            {
                shoppingCartMapper.del(dishId,setmealId,userId);
            }
        }
    }

    /**
     * 清空购物车
     */
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }
}
